import yaml
import time
import random
import requests
def get_headers(config):
    headers = {
        "Cookie": config['cookie'],
        "User-Agent": config['user-agent']
    }
    return headers
def get_params(config):
    url = "https://mp.weixin.qq.com/cgi-bin/appmsgpublish"
    begin = "0"
    params = {
        
        "action": "list_ex", 
        "begin": begin,
        
        "count": "5",
        "fakeid": config['fakeid'],
        "type": "9",
        "token": config['token'],
        "lang": "zh_CN",
        "f": "json",
        "ajax": "1"
    }
    return params
def get_article_list(headers, params):
    i = 0
    url="https://mp.weixin.qq.com/cgi-bin/appmsg"
    column_name = "title,link"
    article_list_path = "article_list.csv"
    with open(article_list_path, "a") as f:
        f.write(column_name + '\n')
    while True:
        begin = i * 5
        params["begin"] = str(begin)
        # 随机暂停几秒，避免过快的请求导致过快的被查到
        time.sleep(random.randint(1,10))
        resp = requests.get(url, headers=headers, params = params, verify=False)
        #print(resp.json())
        
        # 微信流量控制, 退出
        if resp.json()['base_resp']['ret'] == 200013:
            print("frequencey control, stop at {}".format(str(begin)))
            time.sleep(3600)
            continue

        if i >= 532:#此处输入公众号文章的总页数
            break
        for article_list in resp.json()['app_msg_list']:

            with open(article_list_path, "a") as f:
               f.write(str(str(article_list["title"]).encode('GBK','ignore').decode('GBK'))+","+str(str(article_list["link"].encode('GBK','ignore').decode('GBK'))+'\n'))
        i += 1
def main():
    with open("config.yalm", "r") as file:
        file_data = file.read()
    config = yaml.safe_load(file_data) 
    headers = get_headers(config)
    params = get_params(config)
    get_article_list(headers, params)
if __name__ == '__main__':
    main()